package com.morningstar.flow.antvX6;

import com.morningstar.flow.antvX6.common.Node;
import com.morningstar.flow.dao.entity.FlowProcess;
import com.morningstar.flow.flowable.listener.FlowProcessEventListener;
import com.morningstar.flow.flowable.task.AuditServiceTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author Admin
 */
@Slf4j
public class BpmnConvert {

    public static BpmnModel convert(FlowProcess flowProcess, List<Node> nodes) {

        BpmnModel bpmnModel = new BpmnModel();
        bpmnModel.setTargetNamespace(flowProcess.getProcessGroupName());

        Process process = new Process();
        process.setId(flowProcess.getProcessKey());
        process.setName(flowProcess.getProcessName());

        //流程实例监听器
        ArrayList<EventListener> eventListeners = new ArrayList<>();
        EventListener eventListener = new EventListener();
        eventListener.setImplementationType("class");
        eventListener.setImplementation(FlowProcessEventListener.class.getCanonicalName());
        eventListeners.add(eventListener);
        process.setEventListeners(eventListeners);

        //转换节点
        nodes.forEach(node -> {
            node.convertAndAdd(process);
        });

        // 构建用户审批任务 和 服务任务之间的连线
        Collection<FlowElement> flowElements = process.getFlowElements();
        List<FlowElement> userTasks = new ArrayList<>();
        for (FlowElement flowElement : flowElements) {
            if (flowElement instanceof UserTask) {
                userTasks.add(flowElement);
            }
        }
        List<FlowElement> serviceTasks = new ArrayList<>();
        for (FlowElement userTask : userTasks) {
            for (FlowElement flowElement : flowElements) {
                if (flowElement instanceof SequenceFlow sequenceFlow
                        && StringUtils.equalsIgnoreCase(userTask.getId(), sequenceFlow.getSourceRef())) {
                    // 构建服务任务
                    ServiceTask serviceTask = new ServiceTask();
                    serviceTask.setId("approve_service_task_" + userTask.getId());
                    serviceTask.setName(userTask.getName() + "服务任务");
                    serviceTask.setImplementationType("class");
                    serviceTask.setImplementation(AuditServiceTask.class.getCanonicalName());
                    serviceTask.setAsynchronous(false);
//                    process.addFlowElement(serviceTask);
                    serviceTasks.add(serviceTask);
                    // 构建服务任务 和 用户任务之间的连线
                    String targetRef = sequenceFlow.getTargetRef();
                    sequenceFlow.setTargetRef(serviceTask.getId());
                    SequenceFlow serviceTaskSequenceFlow = new SequenceFlow();
                    serviceTaskSequenceFlow.setId(serviceTask.getId() + "_" + userTask.getId());
                    serviceTaskSequenceFlow.setSourceRef(serviceTask.getId());
                    serviceTaskSequenceFlow.setTargetRef(targetRef);
                    serviceTasks.add(serviceTaskSequenceFlow);
//                    process.addFlowElement(sequenceFlow1);
                }
            }
        }
        flowElements.addAll(serviceTasks);
        bpmnModel.addProcess(process);
        return bpmnModel;
    }
}
